﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Device.Location;

namespace MonitoringFloty_WindowsPhoneClient
{
    public partial class GpsPage : PhoneApplicationPage
    {
        GeoCoordinateWatcher watcher;

        public GpsPage()
        {
            InitializeComponent();
            watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default)
            {
                MovementThreshold=20
            };

            watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
            watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
            watcher.Start();
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            watcher.Stop();
            //e.Cancel = true;
            //base.OnBackKeyPress(e);
        }

        void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            Status.Text = e.Status.ToString();
        }

        void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            LastMovement.Text = e.Position.Timestamp.ToString();
            GeoCoordinate coords = e.Position.Location;
            Longtitude.Text = coords.Longitude.ToString();
            Latitude.Text = coords.Latitude.ToString();
            positionUpdater posUpdater = positionUpdater.Instance;
            posUpdater.updatePosition(coords);
        }


    }
}